define(['jquery',
         'global',
         'module/util',
         'bootbox.min',
		 'underscore',
         'bootstrap-datetimepicker.zh-CN',
         'bootstrap-datetimepicker',
         'module/ajax',
         'jquery-weui',
         'bootstrap',
         'pixeladmin'], 
function($, global, util, bootbox, _){
	window.PixelAdmin.start();
	var IsbnOrder = {
		doSave : function(index) {// 保存数据
			$.ajax({
		        url : global.context + '/web/isbnSaveAndUpdate/orderSaveAndUpdate',
		        data : JSON.stringify(IsbnOrder.dtoList[index]),
	            type : 'POST',
	            dataType : 'json',
	            contentType : 'application/json;charset=utf-8',
		        success : function(result) {
		        	if(result.code == "NACK") {
			        	$.growl.warning({title:"提示",message: "保存失败！"});
		        	} else {
			        	$.growl.warning({title:"提示",message: "保存成功！"});
		        	}
		        },
		        error : function(e) {
		        	$.growl.warning({title:"提示",message: "保存失败！"});
		        }
			});
			return this;
		},
		getHtmlData : function(isbnInformationId, index, id) {
			$("#ORDER_PANEL_" + isbnInformationId + "_" + id).find(this.inputConfig).each(function() {
				IsbnOrder.dtoList[index][this.name] = this.value;
			});
			return this;
		},
		// 打开页面检查是否有barcode，如果有则查询，没有则新建
		doLoad : function(obj) {// 初始化页面
			if(!obj || !obj.root || obj.root.length == 0 || !obj.barcode || obj.barcode.length == 0) {
				$.alert("不能缺少基本参数，请重试！！！");
	        	if(obj && obj.error) obj.error();
				return false;
			}
			IsbnOrder.doGetOrder(util.copyBean(obj, {
				beforeSend : function() {//重写befondSend
					$("#" + obj.root).find(".weui-infinite-scroll").show();
		        	if(obj && obj.beforeSend) obj.beforeSend();
				}, 
				success : function(result){//重写success
	        		if(result && result.code == "SUCCESS") {
	        			IsbnOrder.doCreateHtml(util.copyBean(obj, {data : result.data}));
			        	if(obj && obj.success) obj.success();
	        		} else {
						$.alert("加载数据出现错误请重试！！！");
	        		}
				}, 
				error : function(result) {//重写error
		        	if(obj && obj.error) obj.error();
        			$.alert("加载数据出现错误请重试！！！");
				},
				complete : function(result) {
					$("#" + obj.root).find(".weui-infinite-scroll").hide();
				}
			}));			
			return this;
		},
		doLoadSelect : function(isbnInformationId, id) {
			this.initSelect(isbnInformationId, id, "status", "状态", "ISBN_ORDER_STATUS");
			return this;
		},
		doCreateHtml : function(obj) {
			var root = $("#" + obj.root);
			var render = _.template($("#isbnOrderTemplate").html());
			if($.isEmptyObject(obj.data)) {
        		obj.data = [{barcode : obj.barcode, isbnInformationId : obj.isbnInformationId}];
			} 
			for(var i in obj.data) {
				var dto = obj.data[i];
				var index = $(".isbn-order").length;
				var id = (dto.id || index);
				var renderHtml = render({index : index, id : id, barcode : dto.barcode, isbnInformationId : obj.isbnInformationId});
				root.append(renderHtml).find(".weui-infinite-scroll").hide();
				this.doLoadHtmlData(index, id, dto);
			}
			return this;
		},
		doLoadHtmlData : function(index, id, dto) {
			this.doLoadSelect(dto.isbnInformationId, id);
			if(!this.dtoList[index]) {// 加载数据到变量
				this.dtoList[index] = dto;
			}
			$("#ORDER_PANEL_" + dto.isbnInformationId + "_" + id).find(this.inputConfig).val(function() {
				return dto[$(this).attr('name')];
			});
			return this;
		},
		doGetOrder : function(obj) {// 查询数据
			$.ajax({
		        url : global.context + '/web/isbnorder/getOrderData',
		        data : {barcode : obj.barcode, isbnInformationId : obj.isbnInformationId},
	            type : 'POST',
	            dataType : 'json',
	            beforeSend : function() {
		        	if(obj && obj.beforeSend) obj.beforeSend();
	            },
		        success : function(result) {
		        	if(obj && obj.success) obj.success(result);
		        },
		        error : function(result) {
		        	if(obj && obj.error) obj.error(result);
		        },
		        complete : function(result) {
		        	if(obj && obj.complete) obj.complete(result);
		        }
			});
			return this;
		},
		toEditor : function(isbnInformationId, id) {
			$("#ORDER_PANEL_" + isbnInformationId + "_" + id).find(this.inputConfig).each(function() {
				$(this).attr("readonly", false).attr("disabled", false);
			});
			$("#editorOrderBtn_" + id).hide();
			$("#saveOrderBtn_" + id).show();
			$("#cancelEditorOrderBtn_" + id).show();
			this.managerJudge(isbnInformationId, id);
			return this;
		},
		toCancelEditor : function(isbnInformationId, id) {
			$("#ORDER_PANEL_" + isbnInformationId + "_" + id).find(this.inputConfig).each(function() {
				$(this).attr("readonly", true).attr("disabled", true);
			});
			$("#cancelEditorOrderBtn_" + id).hide();
			$("#saveOrderBtn_" + id).hide();
			$("#editorOrderBtn_" + id).show();
			return this;
		},
		// 权限处理
		managerJudge : function(isbnInformationId, id) {
			var index = $("#ORDER_PANEL_" + isbnInformationId + "_" + id).attr("order-index");
			var status = this.dtoList[index].status;
			if(status == 3) {
				//$("#ORDER_PANEL_" + isbnInformationId + "_" + id).find("select[name='status']").attr("readonly", true).attr("disabled", true);		
			}
			return this;
		},		
		// 初始化下拉框
		initSelect : function(isbnInformationId, id, name, nameDesc, dictName) {
			util.selectPadding({
				id : $("#ORDER_PANEL_" + isbnInformationId + "_" + id).find("select[name='"+ name +"']"),
				data : global.dict[dictName],
				firstValue : '',
				firstName : '请选择' + nameDesc
			});
			return this;
		},
		doValidate : function(obj) {
			var arr = $("#ORDER_PANEL_" + obj.isbnInformationId + "_" + obj.id).find(IsbnOrder.inputConfig);
			for(var i = 0; i < arr.length; i ++) {//这里不要用jquery的each迭代
				var $this = $(arr[i]);
				if($this.attr("check") == "must-text" && ($this.val() == null || $this.val().length == 0)) {
					$.alert($this.attr("title") + "必填项");
					$this.focus();
					return false;
				} else if($this.attr("check") == "must-check" && ($this.val() == null || $this.val() == "" || $this.val() == "-1")) {
					$.alert($this.attr("title") + "必选项");
					$this.focus();
					return false;
				}
			}
			if(obj && obj.submit){
				obj.submit();
			}
			return this;
		},
		bindEvent : function() {
			$("body").on('click','.editorOrderBtn',function(){
				IsbnOrder.toEditor($(this).attr("info-id"), $(this).attr("order-id"));
			});
			$("body").on('click','.cancelEditorOrderBtn',function(){
				IsbnOrder.toCancelEditor($(this).attr("info-id"), $(this).attr("order-id"));
			});
			$("body").on('click','.saveOrderBtn',function(){
				var isbnInformationId = $(this).attr("info-id");
				var id = $(this).attr("order-id");
				var index = $(this).attr("order-index");
				IsbnOrder.getHtmlData(isbnInformationId, index, id);
				IsbnOrder.doValidate({
					isbnInformationId : isbnInformationId,
					id : id,
					submit : function() {
						$.confirm({
							title: '确认保存？',
							text: '你确定需要保存数据？',
							onOK: function () {
								IsbnOrder.doSave(index);
							}
						});						  
					}
				});
			});
			return this;
		},
		// init start
		init : function(obj) {
			this.dtoList = {};
			this.maxDataId = 0;
			this.inputConfig = "input[type='hidden'], input[type='text'], input[type='number'], select, textarea";
			this.dictConfig = ["ISBN_ORDER_STATUS"];
			this.bindEvent();

			global.doInitDictData({
				data : IsbnOrder.dictConfig,
			});
			return this;
		}
		// init end			
	};
	IsbnOrder.init();
	return IsbnOrder;
});